﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using GestionPedago.Services;
using GestionPedago.Services.ES;
using GestionPedago.Services.ES.Modele;
using GestionPedago.View;
using System.ComponentModel;
using System.Windows.Data;

namespace GestionPedago.ViewModel
{

    /// <summary>
    /// Classe qui représente l'onglet de planification par stagiaire
    /// </summary>
    public class PlanifierECFStagiaireVM : OngletVM
    {

        private ICommand savePlaningsCommand;

        public ICommand SavePlaningsCommand
        {
            get
            {
                if (this.savePlaningsCommand == null)
                    this.savePlaningsCommand = new RelayCommand(param => this.EnregistrerPlanings());

                return this.savePlaningsCommand;
            }
        }

        /// <summary>
        /// Action d'enregistrement des ECF validés
        /// </summary>
        public void EnregistrerPlanings()
        {
            bool dateNull = false;
            string message = "";
            foreach(PlanningECFDetailES pe in ECFs) {
                if(pe.DejaPlanifie && pe.DateSuggeree == null) {
                    dateNull = true;
                    message = "L'ECF " + pe.PlanningECF.ECF.Libelle + " n'a pas de date de planification.";
                    break;
                }
            }

            if(dateNull) {
                MessageBox.Show(message, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            } else
            if (MessageBox.Show("Voulez vous enregistrer les modifications ?", "Enregistrer", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                try
                {
                    Parent.vmMainWindow.vmBarreInfos.Message = "Enregistrement en cours ...";

                    foreach (PlanningECFDetailES pe in ECFs)
                    {
                        if (pe.PlanningECF.CodeContactENI != null)
                        {
                            pe.PlanningECF.ContactENI = Correcteurs.Single(c => c.CodeContactENI == pe.PlanningECF.CodeContactENI);
                        }
                    }
                    FacadeServices.GetInstance().PlanifierPlanningECFDetail(new PlanifierPlanningECFDetailIn(ECFs.ToList(), PlaningIndivFormation));
                    ECFs = new ObservableCollection<PlanningECFDetailES>(FacadeServices.GetInstance().RecupererPlanningECFDetail(new RecupererPlanningECFDetailIn(this.PlaningIndivFormation)).ListePlanningECFDetail);

                    Parent.RefreshAfterPlanificationsStagiaire();
                    Parent.vmMainWindow.vmBarreInfos.Message = "Enregistrement correctement effectué.";
                }
                catch (Exception e)
                {
                    MessageBox.Show("Erreur lors de l'enregistrement :\n" + e.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private PlanningIndividuelFormationES pPlaningIndivFormation;
        /// <summary>
        /// Planing individuel de formation sélectionné
        /// </summary>
        public PlanningIndividuelFormationES PlaningIndivFormation
        {
            get { return pPlaningIndivFormation; }
            set
            {
                if (value != null)
                {
                    pPlaningIndivFormation = value;
                    ECFs = new ObservableCollection<PlanningECFDetailES>(FacadeServices.GetInstance().RecupererPlanningECFDetail(new RecupererPlanningECFDetailIn(this.pPlaningIndivFormation)).ListePlanningECFDetail);
                    RaisePropertyChanged("PlaningIndivFormation");
                }

            }
        }

        private ObservableCollection<PlanningECFDetailES> pECFs;
        /// <summary>
        /// Liste des planifications d'ECF
        /// </summary>
        public ObservableCollection<PlanningECFDetailES> ECFs
        {
            get
            {
                if (pECFs == null && this.PlaningIndivFormation != null)
                    pECFs = new ObservableCollection<PlanningECFDetailES>(FacadeServices.GetInstance().RecupererPlanningECFDetail(new RecupererPlanningECFDetailIn(this.PlaningIndivFormation)).ListePlanningECFDetail);

                return pECFs;
            }
            set
            {
                pECFs = value;
                RaisePropertyChanged("ECFs");
            }
        }

        private ObservableCollection<ContactENIES> pCorrecteurs;
        /// <summary>
        /// Liste des correcteurs affichés dans la liste déroulante
        /// </summary>
        public ObservableCollection<ContactENIES> Correcteurs
        {
            get { return pCorrecteurs; }
            set
            {
                pCorrecteurs = value;
                RaisePropertyChanged("Correcteurs");
            }
        }


        private object pSelectedECF;
        public object SelectedECF {
            get { return pSelectedECF; }
            set {
                pSelectedECF = value;
                if(pSelectedECF != null) {
                    PlanningECFDetailES plECF = pSelectedECF as PlanningECFDetailES;
                    ListeResultats = new ObservableCollection<ValidationCompetenceES>(FacadeServices.GetInstance().RecupererValidationCompetences(new RecupererValidationCompetencesIn(plECF)).ListeValidationCompetences);
                }
                RaisePropertyChanged("SelectedECF");
            }
        }


        private ObservableCollection<ValidationCompetenceES> pListeResultats;
        public ObservableCollection<ValidationCompetenceES> ListeResultats {
            get { return pListeResultats; }
            set {
                pListeResultats = value;
                RaisePropertyChanged("ListeResultats");
            }
        }


        private ICommand editResultCommand;
        public ICommand EditResultCommand {
            get {
                if(this.editResultCommand == null)
                    this.editResultCommand = new RelayCommand(param => this.EditerResultat());

                return this.editResultCommand;
            }
        }

        private void EditerResultat() {

            if(SelectedResult != null) {
                EditResultat addwindow = new EditResultat(SelectedResult.Statut);
                bool? result = addwindow.ShowDialog();
                if(result ?? false) {
                    SelectedResult.Statut = addwindow.Resultat;
                    SelectedResult = FacadeServices.GetInstance().EnregistrerValidationCompetence(new EnregistrerValidationCompetenceIn(SelectedResult)).ValidationCompetence;

                    ICollectionView view = CollectionViewSource.GetDefaultView(ListeResultats);
                    view.Refresh();

                }
            }

        }

        private ValidationCompetenceES selectedResult;
        public ValidationCompetenceES SelectedResult {
            get { return selectedResult; }
            set {
                selectedResult = value;
                RaisePropertyChanged("SelectedResult");
            }
        }


        /// <summary>
        /// Constructeur de la classe PlanifierECFStagiaireVM
        /// </summary>
        /// <param name="parent">Gestionnaire d'onglets</param>
        public PlanifierECFStagiaireVM(GestionnaireOngletsVM parent)
        {
            base.DisplayName = "Gérer les ECF pour " + parent.vmMainWindow.CurrentStagiaire.Prenom + " " + parent.vmMainWindow.CurrentStagiaire.Nom;
            base.Parent = parent;
            Correcteurs = new ObservableCollection<ContactENIES>(FacadeServices.GetInstance().RecupererFormateurs(new RecupererFormateursIn()).ListeFormateurs);
        }

    }
}
